package com.ophiux.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ophiux.common.core.base.Result;
import com.ophiux.common.core.enums.ResultEnum;
import com.ophiux.member.mapper.MemberCardCollectMapper;
import com.ophiux.member.model.dto.MemberCardCollectDTO;
import com.ophiux.member.model.entity.MemberCardCollect;
import com.ophiux.member.model.vo.MemberCardCollectAddVo;
import com.ophiux.member.model.vo.MemberCardCollectQueryVo;
import com.ophiux.member.model.vo.MemberCardCollectUpdateVo;
import com.ophiux.member.service.IMemberCardCollectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class MemberCardCollectServiceImpl extends ServiceImpl<MemberCardCollectMapper, MemberCardCollect> implements IMemberCardCollectService {
	@Autowired
	MemberCardCollectMapper memberCardCollectMapper;
	

	@Override
	public IPage<MemberCardCollectDTO> getPage(IPage<MemberCardCollect> iPage, MemberCardCollectQueryVo queryDTO) {
		return memberCardCollectMapper.extXmlGetPage(iPage, queryDTO);
	}

	@Override
	public Result insert(MemberCardCollectAddVo vo) {
		if(ObjectUtils.isEmpty(vo.getMemberId())) return Result.fail("会员不能为空");
		if(ObjectUtils.isEmpty(vo.getMemberCardId())) return Result.fail("收藏名片不能为空");
		
		LambdaQueryWrapper<MemberCardCollect> lambdaQueryWrapper = new LambdaQueryWrapper<MemberCardCollect>();
		lambdaQueryWrapper.eq(MemberCardCollect::getMemberId, vo.getMemberId());
		lambdaQueryWrapper.eq(MemberCardCollect::getCollectMemberCardId,vo.getMemberCardId());
		MemberCardCollect data = getOne(lambdaQueryWrapper);
		if(data != null) return Result.fail("此名片已经收藏，请勿重新收藏");

		data = new MemberCardCollect();
		BeanUtils.copyProperties(vo,data);
		int effCount = this.baseMapper.insert(data);
		if (effCount > 0) return Result.success();
		return Result.fail();
	}

	@Override
	public Result update(MemberCardCollectUpdateVo vo) {
		MemberCardCollect data = this.getById(vo.getId());
		if(data == null) return Result.fail(ResultEnum.DATA_NOT_FOUND);
		BeanUtils.copyProperties(vo, data);

		boolean isSuccess = this.updateById(data);
		if(!isSuccess) return Result.fail(ResultEnum.DATA_UPDATE_FAIL);
		return Result.success();
	}

	@Override
	@Transactional
	public Result delete(String ids) {
		List<Integer> list = Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList());
		this.baseMapper.deleteBatchIds(list);
		return Result.success();
	}

	@Override
	public Result<Integer> getCollectMyCount(Integer id) {
		int count = memberCardCollectMapper.getCollectMyCount(id);
		return Result.success(count);
	}

	@Override
	public Result<Integer> getMyCollectCount(Integer id) {
		int count = memberCardCollectMapper.getMyCollectCount(id);
		return Result.success(count);
	}
}
